//
//  WeCloudManager.m
//  WeCloudSDK
//
//  Created by Navinfo on 2018/4/4.
//  Copyright © 2018年 navinfo. All rights reserved.
//

#import "WeCloudManager.h"
#import "NIKeychain.h"
#import "YYModel.h"

#define MOBILEID_KEY @"MOBILEID"
#define DEVICETOKEN_KEY @"DEVICETOKEN"

@implementation NIMessageObject

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"messageId" : @"messageId",
             @"messageType" : @"messageType"
             };
}

@end



@implementation NIRemoteMessageObject

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"resultCode" : @"messageData.resultCode",
             @"resultMessage" : @"messageData.resultMessage",
             @"transactionId" : @"messageData.transactionId",
             @"vin" : @"messageData.vin"
             };
}

@end

static WeCloudManager *shareManager = nil;

@implementation WeCloudManager{
    /**
     正在执行的命令
     */
//    NSMutableArray *runningCommandArray;
    
}

/**
 获取单例对象
 
 @return 返回WeCloudManager单例对象
 */
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[WeCloudManager alloc]init];
    });
    return shareManager;
}



- (instancetype)init
{
    self = [super init];
    if (self) {
//        runningCommandArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}


#pragma mark - 需要保存的唯一标示

/**
 返回手机的唯一标示
 
 @return 手机唯一标示
 */
- (NSString *)mobileID{
    if([NIKeychain load:MOBILEID_KEY] && ![[NIKeychain load:MOBILEID_KEY] isEqualToString:@""]){
        return [NIKeychain load:MOBILEID_KEY];
    }else{
        NSString *mobileKey = [[NSUUID UUID] UUIDString];
        [NIKeychain save:MOBILEID_KEY data:mobileKey];
        return mobileKey;
    }
}


/**
 返回推送的设备id
 
 @return 设备id
 */
- (NSString *)deviceId{
    if([NIKeychain load:DEVICETOKEN_KEY] && ![[NIKeychain load:DEVICETOKEN_KEY] isEqualToString:@""]){
        return [NIKeychain load:DEVICETOKEN_KEY];
    }
    return nil;
}


/**
 存储设备id
 
 @param deviceId 设备id
 */
-(void)setDeviceToken:(NSString *)deviceId{
    NSString *oldToken = [self deviceId];
    if(deviceId && ![deviceId isEqualToString:@""]){
        if (!oldToken) {
            oldToken = @"";
        }
        if(![deviceId isEqualToString:oldToken]){
            [NIKeychain save:DEVICETOKEN_KEY data:deviceId];
        }
    }
}

-(void)setDeviceTokenWithData:(NSData *)deviceIdData{
    if (deviceIdData) {
        NSString *token = [NSString stringWithFormat:@"%@",deviceIdData];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        NSLog(@"日志——获得推送设备号:%@",token);
        [self setDeviceToken:token];
    }
}

- (void)didApnsWork:(NSDictionary *)userInfo{
    NSDictionary *messageDic = [[userInfo objectForKey:@"aps"] objectForKey:@"msg"];
    NSLog(@"日志——获得推送:%@",messageDic);
    NIMessageObject *messageObject = [NIMessageObject yy_modelWithDictionary:messageDic];
    switch (messageObject.messageType) {
        case 1:
        case 2:
            self.remoteMessage = [NIRemoteMessageObject yy_modelWithDictionary:messageDic];
            break;
            
        default:
            break;
    }
}

//#pragma - mark 指令管理
///**
//这辆（帐号下所有车辆）是否还有指令执行
//
//@return 有或没有
//*/
//- (BOOL)isCommandRunning{
//    if (runningCommandArray.count > 0) {
//        return YES;
//    }
//    return NO;
//}
//
//
///**
// 判断指定车辆的指定操作是否正在执行
//
// @param vin 车辆vin
// @param type 指令类型
// @return 是或不是
// */
//- (BOOL)isCommandRunning:(NSString *)vin commandType:(enum NI_COMMAND_TYPE)type{
//    @synchronized (runningCommandArray) {
//        for (NSString *tmpStr in runningCommandArray) {
//            if ([tmpStr isEqualToString:[NSString stringWithFormat:@"%@%d",vin,type]]) {
//                return YES;
//            }
//        }
//    }
//    return NO;
//}
//
//
///**
// 将刷新车况指令加入管理
//
// @param vin 车辆vin
// */
//- (void)addRunningVehicleDataRefreshCommand:(NSString *)vin{
//    [runningCommandArray addObject:[NSString stringWithFormat:@"%@refresh",vin]];
//}
//
//
///**
// 从管理列表中删除刷新车况指令
//
// @param vin 车辆vin
// */
//- (BOOL)removeRunningVehicleDataRefreshCommand:(NSString *)vin{
//    NSArray *tmpArray = [runningCommandArray copy];
//    for (NSString *tmpStr in tmpArray) {
//        if ([tmpStr isEqualToString:[NSString stringWithFormat:@"%@refresh",vin]]) {
//            [runningCommandArray removeObject:tmpStr];
//            return YES;
//        }
//    }
//    return NO;
//}
//
//
///**
// 判断对应车辆是否在执行车况刷新
//
// @param vin 车辆vin
// @return 是或否
// */
//- (BOOL)isVehicleDataRefreshCommandRunning:(NSString *)vin{
//    for (NSString *tmpStr in runningCommandArray) {
//        if ([tmpStr isEqualToString:[NSString stringWithFormat:@"%@refresh",vin]]) {
//            return YES;
//        }
//    }
//    return NO;
//}
//
///**
// 将远程控制指令加入管理
//
// @param vin 车辆vin type 指令类型
// */
//- (void)addRunningCommand:(NSString *)vin commandType:(NICommandType)type{
//    [runningCommandArray addObject:[NSString stringWithFormat:@"%@%d",vin,type]];
//}
//
//
///**
// 从管理列表中删除远程控制指令
//
// @param vin 车辆vin
// @param type 指令类型
// */
//- (BOOL)removeRunningCommand:(NSString *)vin commandType:(NICommandType)type{
//    NSArray *tmpArray = [runningCommandArray copy];
//    for (NSString *tmpStr in tmpArray) {
//        if ([tmpStr isEqualToString:[NSString stringWithFormat:@"%@%d",vin,type]]) {
//            [runningCommandArray removeObject:tmpStr];
//            return YES;
//        }
//    }
//    return NO;
//}

@end
